// Author: Domien Nowicki

#include <iostream>
#include "Game.h"
#include "Color.h"

using namespace std;


Game::Game(const Properties& newProperties): active(false), properties(newProperties), coloringMap(false), allowHints(false), allowColorMap(false)
{
}

Game::Game(const Game& rhs) 
{
	
}

Game::~Game()
{
}

const Properties& Game::GetProperties()
{
	return properties;
}

void Game::Start()
{
	active = true;

	Started();
}

void Game::Started()
{
}

void Game::Stopped()
{
}

std::string Game::GetPlayerPrefix(const Player* player) const
{
	std::string playerPrefix;

	if (player->IsHuman())
		playerPrefix = "(player) ";
	else
		playerPrefix = "(computer) ";

	return playerPrefix;
}

bool Game::DoMove(Player *player, const Move& move)
{
	bool result = false;

	if (!IsActive())
		return result;

	Color color = move.GetColor();
	Map* map = move.GetMap();
	Land* land = map->GetLand(move.GetLand());
	Province* province = land->GetProvince(move.GetProvince());
	std::string playerPrefix = GetPlayerPrefix(player);

	Color provinceColor = province->GetColor();

	// Check neighnours of province, and see if we can color the province with the requested color
	if (provinceColor.IsColor() && color.IsColor())
		Output(playerPrefix + "Cannot overwrite an existing color, please erase the color of province "+move.GetProvince()+" first.");
	else if ((provinceColor.IsColor() && !color.IsColor()) || (!provinceColor.IsColor() && color.IsColor()) )
	{
		Output(playerPrefix + move.ToString());

		province->SetColor(color);

		// check if land is filled in
		if (land->IsCompleted())
			Output(playerPrefix+"Land "+move.GetLand()+" fully colored.");

		player->StepIncrease();

		if (player->IsHuman())
			ShowPlayerStepCount(player->GetStepCount());

		if (map->IsCompleted())
			EndGame(player);
		else
		{
			SwitchTurn();
			result = true;
		}
	}

	return result;
}

bool Game::IsActive() const
{
	return active;
}

void Game::Stop()
{
	active = false;

	Output("Game stopped.");

	Stopped();
}

void Game::EndGame(const Player *player)
{
	std::string winner;
	
	
	if (player->IsHuman())
	{
		winner = "Player";
	}
	else
	{
		winner = "Computer";
	}

	Output(winner+" has won the game!");

	Stop();
}


void Game::Output(std::string message)
{
	cout << message << endl;
}

void Game::SetColoringMap(bool state)
{
	coloringMap = state;
}

bool Game::IsColoringMap() const
{
	return coloringMap;
}

void Game::SetAllowHints(bool state)
{
	allowHints = state;
}

void Game::SetAllowColorMap(bool state)
{
	allowColorMap = state;
}

bool Game::isHintsAllowed() const
{
	return allowHints;
}

bool Game::isColorMapAllowed() const
{
	return allowColorMap;
}

void Game::ShowPlayerStepCount(int steps)
{
}

void Game::ColorMap()
{
}

void Game::Hint()
{
}

void Game::SwitchTurn()
{
}


